{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "! g++ -std=c++20 -O3 -march=native -Wall -Wextra cpu_affinity_bench.cpp -lbenchmark -lpthread -o cpu_affinity_bench"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-10-09T23:18:00+08:00\n",
      "Running ./cpu_affinity_bench\n",
      "Run on (16 X 3792.92 MHz CPU s)\n",
      "CPU Caches:\n",
      "  L1 Data 32 KiB (x8)\n",
      "  L1 Instruction 32 KiB (x8)\n",
      "  L2 Unified 1024 KiB (x8)\n",
      "  L3 Unified 16384 KiB (x1)\n",
      "Load Average: 0.36, 0.30, 0.19\n",
      "[Pinned] 基准线程已绑定 core0\n",
      "-----------------------------------------------------------------------------------\n",
      "Benchmark                         Time             CPU   Iterations UserCounters...\n",
      "-----------------------------------------------------------------------------------\n",
      "BM_Work_Pinned                0.129 ms        0.129 ms         5414 items_per_second=7.73574k/s\n",
      "[Unpinned] 基准线程保持默认调度策略\n",
      "BM_Work_Unpinned              0.129 ms        0.129 ms         5364 items_per_second=7.78119k/s\n",
      "BM_ThreadSwitch_Pinned         10.8 ms         7.70 ms           78 items_per_second=1.29867M/s\n",
      "BM_ThreadSwitch_Unpinned       35.3 ms         17.6 ms           40 items_per_second=567.728k/s\n"
     ]
    }
   ],
   "source": [
    "! ./cpu_affinity_bench"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-10-09T23:17:17+08:00\n",
      "Running ./cpu_affinity_bench\n",
      "Run on (16 X 3792.92 MHz CPU s)\n",
      "CPU Caches:\n",
      "  L1 Data 32 KiB (x8)\n",
      "  L1 Instruction 32 KiB (x8)\n",
      "  L2 Unified 1024 KiB (x8)\n",
      "  L3 Unified 16384 KiB (x1)\n",
      "Load Average: 0.32, 0.30, 0.20\n",
      "[Pinned] 基准线程已绑定 core0\n",
      "------------------------------------------------------------------------------------------\n",
      "Benchmark                                Time             CPU   Iterations UserCounters...\n",
      "------------------------------------------------------------------------------------------\n",
      "BM_Work_Pinned_mean                  0.129 ms        0.129 ms           10 items_per_second=7.75219k/s\n",
      "BM_Work_Pinned_median                0.129 ms        0.129 ms           10 items_per_second=7.77491k/s\n",
      "BM_Work_Pinned_stddev                0.001 ms        0.001 ms           10 items_per_second=51.9434/s\n",
      "BM_Work_Pinned_cv                     0.67 %          0.68 %            10 items_per_second=0.67%\n",
      "[Unpinned] 基准线程保持默认调度策略\n",
      "BM_Work_Unpinned_mean                0.130 ms        0.130 ms           10 items_per_second=7.69489k/s\n",
      "BM_Work_Unpinned_median              0.130 ms        0.130 ms           10 items_per_second=7.71567k/s\n",
      "BM_Work_Unpinned_stddev              0.001 ms        0.001 ms           10 items_per_second=76.7161/s\n",
      "BM_Work_Unpinned_cv                   1.01 %          1.01 %            10 items_per_second=1.00%\n",
      "BM_ThreadSwitch_Pinned_mean           9.93 ms         6.83 ms           10 items_per_second=1.48987M/s\n",
      "BM_ThreadSwitch_Pinned_median         9.65 ms         6.67 ms           10 items_per_second=1.49945M/s\n",
      "BM_ThreadSwitch_Pinned_stddev         1.81 ms        0.959 ms           10 items_per_second=210.465k/s\n",
      "BM_ThreadSwitch_Pinned_cv            18.25 %         14.03 %            10 items_per_second=14.13%\n",
      "BM_ThreadSwitch_Unpinned_mean         34.4 ms         17.2 ms           10 items_per_second=581.844k/s\n",
      "BM_ThreadSwitch_Unpinned_median       34.7 ms         17.3 ms           10 items_per_second=576.862k/s\n",
      "BM_ThreadSwitch_Unpinned_stddev      0.887 ms        0.439 ms           10 items_per_second=14.9527k/s\n",
      "BM_ThreadSwitch_Unpinned_cv           2.58 %          2.55 %            10 items_per_second=2.57%\n"
     ]
    }
   ],
   "source": [
    "! ./cpu_affinity_bench --benchmark_repetitions=10 --benchmark_report_aggregates_only=true --benchmark_filter=.*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
